package com.cws4cloud.cws.mall.xsm.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cws4cloud.cws.mall.xsm.entity.Tradelog;
import com.cws4cloud.cws.mall.xsm.mapper.TradelogMapper;
import com.cws4cloud.cws.mall.xsm.service.TradelogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 订单日志
 *
 * @author zhulj
 * @date 2021-06-19 19:58:53
 */
@Service
@Slf4j
public class TradelogServiceImpl extends ServiceImpl<TradelogMapper, Tradelog> implements TradelogService {

	@Override
	public boolean saveTradeLog(Object message) {
		Tradelog tradelog = null;
		try {
			tradelog = JSONUtil.toBean(message.toString(), Tradelog.class);
			//判断是否已经存在，已存在直接跳过
			if (this.count(Wrappers.<Tradelog>lambdaQuery().eq(Tradelog::getType, tradelog.getType()).eq(Tradelog::getTradeSeq,tradelog.getTradeSeq()))>0){
				log.warn("订单消息重复，跳过保存，消息内容为：{}",message);
				return true;
			}
		} catch (Exception e){
			log.error("订单消息保存异常原因：", e);
			return false;
		}
		return this.save(tradelog);
	}
}
